package com.itheima;

import org.junit.jupiter.api.Test;

import javax.xml.crypto.Data;
import java.io.*;

public class FileReaderAndWriter {
    @Test
    public void testFileReader() {
        try(
                Reader fr = new FileReader("src/main/java/com/itheima/fileTest.txt");
                BufferedReader br = new BufferedReader(fr);
                ) {
            int c;
            // 单个字符读
//            while ((c = fr.read()) != -1) {
//                System.out.print((char) c);
//            }
//readLine 读行
            char[] buffer = new char[5];
            while ((c = br.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, c));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testFileWriter() {
        try(
                Writer fr = new FileWriter("src/main/java/com/itheima/fileTest.txt", true);
                BufferedWriter bw = new BufferedWriter(fr);
                ) {
            bw.write("哈哈哈");
            bw.newLine();
            bw.write("哈哈哈2");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testPrintStream() {
        try(
                PrintStream ps = new PrintStream(new FileOutputStream("src/main/java/com/itheima/fileTest.txt", true));
                ) {
            ps.println("aaa");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testPrintln() {
        System.out.println("原本打印到控制台的数据，现在打印到文件");
        try(
                PrintStream ps = new PrintStream("src/main/java/com/itheima/fileTest.txt")
                ) {
            System.setOut(ps);
            System.out.println("写进文件");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testDataOut() { // 数据输出流
        try(
                DataOutputStream dos = new DataOutputStream(new FileOutputStream("src/main/java/com/itheima/fileTest.txt"))
                ) {
            dos.writeInt(99);
            dos.writeDouble(99.9);
            dos.writeBoolean(true);
            dos.writeUTF("黑马程序员");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testDataIn() { // 数据输入流
        try(
                DataInputStream dis = new DataInputStream(new FileInputStream("src/main/java/com/itheima/fileTest.txt"))
        ) {
            int i = dis.readInt();
            System.out.println(i);

            double d = dis.readDouble();
            System.out.println(d);

            boolean b = dis.readBoolean();
            System.out.println(b);

            String s = dis.readUTF();
            System.out.println(s);

        } catch (Exception e) {

        }
    }

    @Test
    public void TestObjectOutput() {
        try(
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/main/java/com/itheima/fileTest.txt"))
                ) {
            User u = new User("张三", 20);
            oos.writeObject(u);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Test
    public void testObjectInput() {
        try(
                ObjectInputStream oos = new ObjectInputStream(new FileInputStream("src/main/java/com/itheima/fileTest.txt"))
        ) {
           User u =(User) oos.readObject();
           System.out.println(u);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class User implements Serializable{
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
